<?php
class Custom_Proxy_Curl
{
	public function __construct($cookFile = '', $curlParams = array()) {
		if ($cookFile == '')
			$this->cookieFile = sha1 ( time () . mt_rand ( 1, 99999 ) );
		else
			$this->cookieFile = $cookFile;
		
		$this->Initialize ( $curlParams );
	}
	
	/*public function __construct($cookFile = '', $curlParams = array()) {
		
	}*/
	
	public function Initialize($custom = array()) {
		$this->refCurl = curl_init ();
		$this->_SetCURLOption ( CURLOPT_VERBOSE, 0 );		
		$this->_SetCURLOption ( CURLOPT_CONNECTTIMEOUT, 1000 );
		$this->_SetCURLOption ( CURLOPT_TIMEOUT, 10000 );		
		$this->_SetCURLOption ( CURLOPT_RETURNTRANSFER, 1 );
		$this->_SetCURLOption ( CURLOPT_FOLLOWLOCATION, 1 );
		$this->_SetCURLOption ( CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20061025 Firefox/3.5.6' );
		$this->_SetCURLOption ( CURLOPT_FOLLOWLOCATION, 1 );
		$this->_SetCURLOption ( CURLOPT_AUTOREFERER, 0 );
		$this->_SetCURLOption ( CURLOPT_SSL_VERIFYPEER, 0 );
		$this->_SetCURLOption ( CURLOPT_SSL_VERIFYHOST, 0 );
		
		$this->_SetCURLOption ( CURLOPT_COOKIEJAR, $this->cookieFile );
		$this->_SetCURLOption ( CURLOPT_COOKIEFILE, $this->cookieFile );
		
		if (is_array ( $custom ))
			foreach ( $custom as $c ) {
				if (count ( $c ) == 2)
					$this->_SetCURLOption ( $c [0], $c [1] );
			}
	
	}
	
	public function GetHeaders($url) {
		$this->_SetCURLOption ( CURLOPT_HEADER, 1 );
		$this->_SetCURLOption ( CURLOPT_NOBODY, 1 );
		$ret = $this->GetURL ( $url );
		
		$this->_SetCURLOption ( CURLOPT_HEADER, 0 );
		$this->_SetCURLOption ( CURLOPT_NOBODY, 0 );
		
		return $ret;
	}
	
	public function SetDebug($option = true) {
		if ($option)
			$this->_SetCURLOption ( CURLOPT_VERBOSE, $option );
		else
			$this->_SetCURLOption ( CURLOPT_VERBOSE, $option );
	}
	
	public function GetCookieFileName() {
		return $this->cookieFile;
	}
	
	public function GetCookieString() {
		
		$this->_SetCURLOption ( CURLINFO_HEADER_OUT, 1 );
		$str = $this->GetTransferInfo ( CURLINFO_HEADER_OUT );
		if (preg_match ( '/Cookie:(.*)/i', $str, $matches )) {
			$this->_SetCURLOption ( CURLINFO_HEADER_OUT, 0 );
			return trim ( $matches [1] );
		}
		
		return '';
	}
	
	public function FreeMemory($deleteCookie = false) {
		curl_close ( $this->refCurl );
		
		if ($deleteCookie && file_exists ( $this->cookieFile ))
			unlink ( $this->cookieFile );
	}
	
	public function DeleteCookie() {
		if (file_exists ( $this->cookieFile ))
			unlink ( $this->cookieFile );
	}
	
	public function GetURL($url, $saveTo = false) {
		$this->_SetCURLOption ( CURLOPT_POST, 0 );
		$this->_SetCURLOption ( CURLOPT_URL, $url );
		
		if ($saveTo) {
			if (file_put_contents ( $saveTo, $this->_ExecCURL () ))
				return true;
			else
				return false;
		} else
			return $this->_ExecCURL ();
	}
	
	public function SetReferer($url = '') {
		$this->_SetCURLOption ( CURLOPT_REFERER, $url );
	}
	
	public function PostURL($url, $data, $forceQueryString = true, $debug = false) {

		$hasFile = false;
		if (is_array ( $data )) {
			foreach ( $data as $dat ) {
				if ($dat {0} == '@') {
					$hasFile = true;
					break;
				}
			}
		} else
			$forceQueryString = false;
		
		if ($forceQueryString && ! $hasFile)
			$data = $this->GetQueryString ( $data, true );
		
		if ($debug)
			print_r ( $data );
		
		$this->_SetCURLOption ( CURLOPT_POST, 1 );
		$this->_SetCURLOption ( CURLOPT_URL, $url );
		$this->_SetCURLOption ( CURLOPT_POSTFIELDS, $data );
		
		return $this->_ExecCURL ();
	}
	
	public function SetOption($identifier, $data) {
		$this->_SetCURLOption ( $identifier, $data );
	}
	
	public function GetTransferInfo($identifier = false) {
		if (! $this->refCurl)
			return array ();
		else {
			if ($identifier) {
				return curl_getinfo ( $this->refCurl, $identifier );
			} else {
				return curl_getinfo ( $this->refCurl );
			}
		}
	}
	
	public function _SetCURLOption($identifier, $data) {
		if (! $this->refCurl)
			die ( "CURL not initialized!" );
		
		curl_setopt ( $this->refCurl, $identifier, $data );
	}
	
	public function _ExecCURL() {
		if (! $this->refCurl)
			die ( "CURL not initialized!" );
		
		return curl_exec ( $this->refCurl );
	}
	
	public function GetQueryString($form_values = array(), $urlencode = true) {
		if (! is_array ( $form_values ))
			return '';
		
		$form_fields = array_keys ( $form_values );
		$form_data = '';
		
		for($i = 0; $i < sizeof ( $form_fields ); $i ++) {
			if ($urlencode) {
				if (is_array ( $form_values [$form_fields [$i]] )) {
					foreach ( $form_values [$form_fields [$i]] as $ff ) {
						$form_data .= urlencode ( $form_fields [$i] ) . '=' . urlencode ( $ff ) . '&';
					}
				} else {
					$form_data .= urlencode ( $form_fields [$i] ) . '=' . urlencode ( $form_values [$form_fields [$i]] );
				}
			} else {
				if (is_array ( $form_values [$form_fields [$i]] )) {
					foreach ( $form_values [$form_fields [$i]] as $ff ) {
						$form_data .= $form_fields [$i] . '=' . $ff . '&';
					}
				} else {
					$form_data .= $form_fields [$i] . '=' . $form_values [$form_fields [$i]];
				}
			}
			
			if ($i != (sizeof ( $form_fields ) - 1)) {
				$form_data = trim ( $form_data, '&' );
				$form_data .= '&';
			}
		}
		
		return $form_data;
	}
	
	public $refCurl = null;
	public $cookieFile = null;
};
?>